package com.sxzbxc.weixin.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sxzbxc.weixin.entity.SysRole;
import com.sxzbxc.weixin.entity.SysUser;
import com.sxzbxc.weixin.pojo.AjaxJson;
import com.sxzbxc.weixin.service.SysRoleServiceI;
import com.sxzbxc.weixin.service.SysUserServiceI;
import com.sxzbxc.weixin.util.PasswordUtil;


/**
 * 个人中心模块
 * @author Tom
 *
 */
@Controller
@RequestMapping("userController")
public class UserController {

	@Autowired
	private SysUserServiceI sysUserService;
	
	@Autowired
	private SysRoleServiceI sysRoleService;
	
	/**
	 * 用户菜单
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(){
		
		return new ModelAndView("user/userList");
	}
	
	/**
	 * 进入修改密码页面
	 * @return
	 */
	@RequestMapping(params = "toUpdatePassWord")
	public ModelAndView toUpdatePassword(){
		
		return new ModelAndView("user/updatePassWord");
	}
	
	/**
	 * 进入修改个人信息页面
	 * @return
	 */
	@RequestMapping(params = "toUpdateUserInfo")
	public ModelAndView toUpdateInfo(){
		
		return new ModelAndView("user/updateInfo");
	}
	
	/**
	 * 更新密码的方法
	 * @return
	 */
	@RequestMapping(params = "doUpdatePassWord")
	@ResponseBody
	public AjaxJson doUpdatePassword(HttpServletRequest request , String newPassWord){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)request.getSession().getAttribute("user");
		try {
			String encrypt = PasswordUtil.encrypt(newPassWord, PasswordUtil.password, PasswordUtil.getSalt());
			user.setPassword(encrypt);
			sysUserService.updateByPrimaryKeySelective(user);
			j.setSuccess(true);
		} catch (Exception e) {
			j.setSuccess(false);
			e.printStackTrace();
		}
		return j;
	}
	
	/**
	 * 添加用户信息任务
	 * @return
	 */
	@RequestMapping(params = "doAddUser1")
	@ResponseBody
	public AjaxJson doAddUser(HttpServletRequest request ,String userName,
			String userKey , String userRole , String userDepart,String phonenum,
			int userStatus1 , String passWord ){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)request.getSession().getAttribute("user");
		SysUser sysUser = new SysUser();
		sysUser.setUserName(userName);
		sysUser.setUserKey(userKey);
		sysUser.setUserRole(userRole);
		sysUser.setPhonenum(phonenum);
		sysUser.setUserDepart(userDepart);
		sysUser.setUserUpdateName(user.getUserName());
		sysUser.setUserUpdateDate(new Date());
		sysUser.setUserStatus1(userStatus1);
		try {
			sysUser.setUserUpdateName(user.getUserName());
			String pwd = PasswordUtil.encrypt(sysUser.getPassword(), PasswordUtil.password, PasswordUtil.getSalt());
			sysUser.setPassword(pwd);
			sysUserService.insertSelective(sysUser);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j ;
	}
	
	/**
	 * 更新用户信息任务
	 * @return
	 */
	@RequestMapping(params = "doUpdateUser")
	@ResponseBody
	public AjaxJson doUpdateUser(HttpServletRequest request , HttpServletResponse response,String id,
			String userName ,String userDepart, String  userKey,String userRole , String phonenum,int userStatus1){
		AjaxJson j = new AjaxJson();
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		sysUser.setPhonenum(phonenum);
		sysUser.setUserDepart(userDepart);
		sysUser.setUserKey(userKey);
		sysUser.setUserName(userName);
		sysUser.setUserRole(userRole);
		sysUser.setUserStatus1(userStatus1);
		SysUser user = (SysUser)request.getSession().getAttribute("user");
		Date date = new Date();
		sysUser.setUserUpdateName(user.getUserUpdateName());
		sysUser.setUserUpdateDate(date);
		try {
			sysUserService.updateByPrimaryKeySelective(sysUser);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j ;
	}
	
	
	/**
	 * 更新个人信息的方法
	 * @return
	 */
	@RequestMapping(params = "doUpdateInfo")
	@ResponseBody
	public AjaxJson doUpdateInfo(HttpServletRequest request , SysUser newPassWord){
		AjaxJson j = new AjaxJson();
		Timestamp date = new Timestamp(0); 
		try {
			SysUser user = (SysUser)request.getSession().getAttribute("user");
			user.setPhonenum(newPassWord.getPhonenum());
			user.setUserKey(newPassWord.getUserKey());
			user.setUserName(newPassWord.getUserName());
			user.setUserUpdateDate(date);
			user.setUserUpdateName(newPassWord.getUserName());
			user.setWxid(newPassWord.getWxid());
			sysUserService.updateByPrimaryKeySelective(user);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return j;
	}
	
	/**
	 * 修改密码——验证密码
	 * @param req
	 * @param oldPassWord
	 * @return
	 */
	@RequestMapping(params = "passwordGrid")
	@ResponseBody
	public AjaxJson passwordGrid(HttpServletRequest req , String oldPassWord){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)req.getSession().getAttribute("user");
		try {
			String decrypt = PasswordUtil.decrypt(user.getPassword(), PasswordUtil.password, PasswordUtil.getSalt());
			if(decrypt.equals(oldPassWord)){
				j.setSuccess(true);
				return j;
			}else{
				j.setSuccess(false);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		j.setSuccess(false);
		return j;
	}
	
	/**
	 * 修改个人信息---验证微信号唯一
	 * @param req
	 * @param wxid
	 * @return
	 */
	@RequestMapping(params = "wxidGrid")
	@ResponseBody
	public AjaxJson wxidGrid(HttpServletRequest req , String wxid){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)req.getSession().getAttribute("user");
		if(user.getWxid().equals(wxid)){
			j.setSuccess(false);
		}else{
			j.setSuccess(true);
		}
		return j;
	}
	
	/**
	 * 修改个人信息---监测手机号
	 * @param req
	 * @param phonenum
	 * @return
	 */
	@RequestMapping(params = "phonenumGrid")
	@ResponseBody
	public AjaxJson phonenumGrid(HttpServletRequest req , String phonenum){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)req.getSession().getAttribute("user");
		if(user.getPhonenum() . equals(phonenum)){
			j.setSuccess(false);
		}else{
			j.setSuccess(true);
		}
		return j;
	}
	
	
	/**
	 * 注销跳转
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "writtenOff")
	public ModelAndView writtenOff (HttpServletRequest request){
		request.getSession().removeAttribute("user");
		return new ModelAndView("../index");
	}
	
	/**
	 * 查询角色
	 * @return
	 */
	@RequestMapping(params = "roleDatagrid")
	@ResponseBody
	public AjaxJson roleDatagrid(){
		AjaxJson j = new AjaxJson();
		try {
			List<SysRole> loadAll = sysRoleService.loadAll();
			if(loadAll.size()==0){
				j.setSuccess(false);
				return j;
			}
			j.setObj(loadAll);
			j.setSuccess(true);
		} catch (Exception e) {
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping(params = "role")
	public ModelAndView role(){
		return new ModelAndView("user/addrole");
	}
	
	@RequestMapping(params = "doadd")
	@ResponseBody
	public AjaxJson doadd(HttpServletRequest request , SysRole role ){
		AjaxJson j = new AjaxJson();
		role.setRoleStatus(1);
		sysRoleService.insertSelective(role);
		return j;
	}
	
	
	@RequestMapping(params ="userDatagrid")
	@ResponseBody
	public AjaxJson userDatagrid(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)req.getSession().getAttribute("user");
		j.setObj(user);
		j.setSuccess(true);
		return j;
	}
}
